<template>
  <div>
    <el-dialog :title="info.isAdd ? '用户添加' : '用户修改'" :visible.sync="info.isshow" @close="close">
      <el-form :model="user">
        <el-form-item label="所属角色" label-width="120px">
          <el-select v-model="user.roleid" placeholder="请选择上级菜单">
            <el-option v-for="item in arr" :key="item.id" :label="item.rolename" :value="item.id"></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="用户名" label-width="120px">
          <el-input v-model="user.username" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="密码" label-width="120px">
          <el-input v-model="user.password" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="状态" label-width="120px">
          <el-switch v-model="user.status" :active-value="1" :inactive-value="2"></el-switch>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cal">取 消</el-button>
        <el-button type="primary" v-if="info.isAdd" @click="add">添 加</el-button>
        <el-button type="primary" v-else @click="update">修 改</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import { mapGetters, mapActions } from "vuex";
import {
  requseradd,
  requserinfo,
  requseredit,
  reqrolelist
} from "../../../http/api";
import { successAlert, errorAlert } from "../../../utils/alert";
import { formClose } from "../../../mixins";

export default {
  props: ["info"],
  mixins: [formClose],
  data() {
    return {
      user: {
        roleid: "",
        username: "",
        password: "",
        status: 1
      },
      arr: []
    };
  },
  computed: {
    ...mapGetters({
      uinfo: "info"
    })
  },
  mounted() {
    reqrolelist().then(res => {
      if (res.data.code === 200) {
        this.arr = res.data.list;
      }
    });
  },
  methods: {
    ...mapActions({
      changeInfo: "changeInfo"
    }),
    checkdata() {
      return new Promise((resolve, reject) => {
        if (this.user.roleid == "") {
          errorAlert("所属角色不能为空");
          return;
        }
        if (this.user.username == "") {
          errorAlert("用户名不能为空");
          return;
        }
        if (this.info.isAdd && this.user.password == "") {
          errorAlert("密码不能为空");
          return;
        }
        resolve();
      });
    },
    add() {
      this.checkdata().then(() => {
        requseradd(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    },
    empty() {
      this.user = {
        roleid: "",
        username: "",
        password: "",
        status: 1
      };
    },
    getOne(id) {
      requserinfo({ uid: id }).then(res => {
        if (res.data.code === 200) {
          this.user = res.data.list;
          this.user.password = "";
        }
      });
    },
    update() {
      this.checkdata().then(() => {
        requseredit(this.user).then(res => {
          if (res.data.code === 200) {
            successAlert(res.data.msg);
            // 如果修改的是本账号的信息，清空用户数据，并跳转至登录
            if (this.user.uid === this.uinfo.uid) {
              this.changeInfo({});
              this.$router.replace("/login");
              return;
            }
            this.empty();
            this.info.isshow = false;
            this.$emit("init");
          }
        });
      });
    }
  }
};
</script>
<style scoped lang='less'>
@import "../../../less/index.less";
</style>